package es.us.lsi.daw.model.security;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;

import es.us.lsi.daw.model.security.enums.AuthorityEnum;
import es.us.lsi.daw.model.util.BaseEntity;



@Entity
@Table(name = Tables.AUTHORITIES)
public class Authority extends BaseEntity implements Serializable, GrantedAuthority {

	
	private static final long serialVersionUID = 1L;

	public static final String A_AUTHORITY = "authority";
	
	@Enumerated(EnumType.STRING)
	private AuthorityEnum authority;
	
	public String toString(){
		return authority.toString();
	}

	public int compareTo(Object authority) {
		return this.authority.toString().compareTo(((GrantedAuthority) authority).getAuthority());
	}

	public String getAuthority() {
		return authority.toString();
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((authority == null) ? 0 : authority.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Authority other = (Authority) obj;
		if (authority == null) {
			if (other.authority != null)
				return false;
		} else if (!authority.equals(other.authority))
			return false;
		return true;
	}
	

}
